<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入用户名称" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile">
            <el-input v-model="queryParams.mobile" placeholder="请输入手机号码" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="用户状态" prop="userStatus">
            <el-select v-model="queryParams.userStatus" placeholder="用户状态" clearable style="width: 240px">
              <el-option label="禁言" value="1" />
              <el-option label="封禁" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:user:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange" height="500" style="overflow-y: auto;">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="用户ID" align="center" key="uid" prop="uid" v-if="columns.uid.visible" />
          <el-table-column label="用户名称" align="center" key="name" prop="name" v-if="columns.name.visible" :show-overflow-tooltip="true" />
          <el-table-column label="显示名称" align="center" key="displayName" prop="displayName" v-if="columns.displayName.visible" :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" key="mobile" prop="mobile" v-if="columns.mobile.visible" width="120" />
          <el-table-column label="邮箱" align="center" key="email" prop="email" v-if="columns.email.visible" :show-overflow-tooltip="true" />
          <el-table-column label="用户状态" align="center" key="userStatus" v-if="columns.userStatus.visible">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.userStatus === 1" type="warning">禁言</el-tag>
              <el-tag v-else-if="scope.row.userStatus === 2" type="danger">封禁</el-tag>
              <el-tag v-else type="info">未知</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="regTime" v-if="columns.regTime.visible" width="160">
          </el-table-column>
          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleStatusUpdate(scope.row)" v-hasPermi="['system:user:edit']">修改状态</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 修改用户状态对话框 -->
    <el-dialog title="修改用户状态" :visible.sync="statusOpen" width="400px" append-to-body>
      <el-form ref="statusForm" :model="statusForm" label-width="80px">
        <el-form-item label="用户名称">
          <el-input v-model="statusForm.userName" :disabled="true" />
        </el-form-item>
        <el-form-item label="用户状态" prop="status">
          <el-radio-group v-model="statusForm.status">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">禁言</el-radio>
            <el-radio :label="2">封禁</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitStatusForm">确 定</el-button>
        <el-button @click="cancelStatus">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDisabledUsers,  setUserStatus } from "@/api/system/tuser"

export default {
  name: "DisabledUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 日期范围
      dateRange: [],
      // 是否显示状态修改弹出层
      statusOpen: false,
      // 状态表单参数
      statusForm: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        mobile: null,
        email: null,
        userStatus: undefined // 用户状态过滤
      },
      // 列信息
      columns: {
        uid: { label: '用户ID', visible: true },
        name: { label: '用户名称', visible: true },
        displayName: { label: '显示名称', visible: true },
        mobile: { label: '手机号码', visible: true },
        email: { label: '邮箱', visible: true },
        userStatus: { label: '用户状态', visible: true },
        regTime: { label: '创建时间', visible: true }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询禁用用户列表 */
    getList() {
      this.loading = true
      // 构建查询参数
      const params = {
        ...this.addDateRange(this.queryParams, this.dateRange)
      }
      
      // 调用专门的禁用用户接口
      listDisabledUsers(params).then(response => {
        this.userList = response.rows || []
        this.total = parseInt(response.total) || 0
        this.loading = false
      }).catch(error => {
        console.error('获取禁用用户列表失败:', error)
        this.userList = []
        this.total = 0
        this.loading = false
        this.$modal.msgError('获取禁用用户列表失败，请检查网络连接或联系管理员')
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []  // 重置日期范围
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改用户状态按钮操作 */
    handleStatusUpdate(row) {
      if (row && row.uid) {
        // 单个用户状态修改
        this.statusForm = {
          uid: row.uid,
          userName: row.name || row.displayName,
          status: row.userStatus || 0
        }
        this.statusOpen = true
      } else {
        this.$modal.msgError("请选择一个用户进行状态修改")
        return
      }
    },
    /** 提交状态修改 */
    submitStatusForm() {
      const statusData = {
        uid: this.statusForm.uid,
        status: this.statusForm.status
      }
      
      setUserStatus(statusData).then(() => {
        this.$modal.msgSuccess("状态修改成功")
        this.statusOpen = false
        this.getList()
      })
    },
    /** 取消状态修改 */
    cancelStatus() {
      this.statusOpen = false
      this.statusForm = {}
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tuser/export', {
        ...this.queryParams
      }, `disabled_user_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>